package command

import (
	"errors"
	"strings"
	"time"

	"ssalarm/common/dto"
	"ssalarm/common/utils"
	"ssalarm/internal/alarm/models"
)

type AlarmConfigUpsertReq struct {
	Sn           string                   `json:"sn" comment:"设备SN"`
	Key          string                   `json:"key" comment:"变量key" vd:"len($)>0"`
	Warning      []map[string]interface{} `json:"warning" comment:"告警配置"`
	Enable       *bool                    `json:"enable"`
	ProductCode  string                   `json:"productCode" comment:"产品编码"`
	DevProductId string                   `json:"devProductId" comment:"产品id"`
	dto.UserInfo `search:"-"`
}

func (d *AlarmConfigUpsertReq) Generate(model *models.SysAlarmConfig) error {
	if len(d.Sn) == 0 && len(d.ProductCode) == 0 {
		return errors.New("参数异常sn/productCode")
	}
	now := dto.LocalTime(time.Now())
	model.DevSn = strings.TrimSpace(d.Sn)
	model.Key = strings.TrimSpace(d.Key)
	model.Enable = true
	model.ProductCode = d.ProductCode
	model.DevProductId = d.DevProductId
	if len(d.ProductCode) > 0 {
		model.DevSn = d.ProductCode
	}
	warning := make([]map[string]interface{}, 0)
	model.Warning = utils.MapArrToBytes(warning)
	model.UpdateTime = &now
	if d.Warning != nil {
		model.Warning = utils.MapArrToBytes(d.Warning)
	}
	if d.Enable != nil {
		model.Enable = *d.Enable
	}
	return nil
}

type AlarmConfigDeleteReq struct {
	Sn           string `json:"sn" comment:"设备SN"`
	ProductCode  string `json:"productCode" comment:"产品编码"`
	Key          string `json:"key" comment:"变量key" vd:"len($)>0"`
	dto.UserInfo `search:"-"`
}
